﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvatarHelper;
using Avatar.Model;

namespace Avatar.ResumeService
{
    public static class JobIntentionManager
    {
        public static void SaveJobIntention(
            string _user,
            int _areaID,
            int _expectationMoney,
            List<int> _industry,
            int _jobType,
            int _positionID,
            string _positionName,
            int _attitude,
            int _timeOnDuty
            )
        {
            try
            {
                #region 入口检测

                if (string.IsNullOrEmpty(_user)) throw new Exception("登录超时 请重新登录");
                if (_industry == null || _industry.Count <= 0) throw new Exception("行业类别不能为空");
                if (string.IsNullOrEmpty(_positionName)) throw new Exception("职位名称必填");
                if (!string.IsNullOrEmpty(_positionName) && _positionName.Length > 20) throw new Exception("职位名称不能超过20个字符");
                
                #endregion

                using (var _adc = DBHelper.GetDataContext())
                {
                    var _resume = _adc.Resume.Where(r => r.User == _user).FirstOrDefault();
                    if (_resume != null)
                    {
                        if (_resume.JobIntention == null)
                        {
                            _resume.JobIntention = new JobIntention();

                            _resume.JobIntention.AreaID = _areaID;
                            _resume.JobIntention.ExpectationMoney = _expectationMoney;
                            _resume.JobIntention.JobTypeID = _jobType;
                            _resume.JobIntention.AttitudeID = _attitude;
                            _resume.JobIntention.TimeOnDutyID = _timeOnDuty;
                            _resume.JobIntention.PositionID = _positionID;
                            _resume.JobIntention.PositionName = _positionName;
                            _resume.JobIntention.ID = Guid.NewGuid();

                            _adc.JobIntention.InsertOnSubmit(_resume.JobIntention);
                            _adc.SubmitChanges();
                            foreach (var _i in _industry)
                            {
                                var _indus = new JobIntentionIndustry();
                                _indus.IndustryID = _i;
                                _indus.IntentionID = _resume.JobIntention.ID;
                                _adc.JobIntentionIndustry.InsertOnSubmit(_indus);
                            }
                            _adc.SubmitChanges();
                        }
                        else
                        {
                            _resume.JobIntention.AreaID = _areaID;
                            _resume.JobIntention.PositionID = _positionID;
                            _resume.JobIntention.PositionName = _positionName;
                            _resume.JobIntention.ExpectationMoney = _expectationMoney;
                            _resume.JobIntention.JobTypeID = _jobType;
                            _resume.JobIntention.AttitudeID = _attitude;
                            _resume.JobIntention.TimeOnDutyID = _timeOnDuty;

                            var _industryOld = _resume.JobIntention.JobIntentionIndustry;
                                foreach (var _i in _industry)
                                {
                                    if(_industryOld.FirstOrDefault(i => i.IndustryID == _i) == null)
                                    {
                                        var _indus = new JobIntentionIndustry();
                                        _indus.IndustryID = _i;
                                        _indus.IntentionID = _resume.JobIntention.ID;
                                        _adc.JobIntentionIndustry.InsertOnSubmit(_indus);
                                    }
                                }
                                _adc.SubmitChanges();

                                foreach (var _i in _industryOld)
                                {
                                    bool _have = false;
                                    foreach (var _ii in _industry)
                                    {
                                        if (_i.IndustryID == _ii)
                                            _have = true;
                                    }
                                    if (_have == false)
                                        _adc.JobIntentionIndustry.DeleteOnSubmit(_i);
                                }
                                _adc.SubmitChanges();
                        }
                        _adc.SubmitChanges();
                    }
                }
            }
            catch (Exception _e)
            {
                throw new Exception("编辑求职意向出错：" + _e.Message);
            }
        }
        public static string GetPosition(string _user)
        {
            string _position = string.Empty;
            using(var _adc=DBHelper.GetDataContext())
            {
                var _resume= _adc.Resume.FirstOrDefault(r => r.User == _user);
                _position = string.IsNullOrEmpty(_resume.JobIntention.PositionName) ? "未设置" : _resume.JobIntention.PositionName;
                //if (_positions.Count > 0)
                //{
                //    _position = "<select>";
                //    foreach (var _i in _positions)
                //    {
                //        _position += "<option>" + _i.Position.Name + "</option>";
                //    }
                //    _position += "</select>";
                //}
                //else
                //{
                //    _position = "未设置";
                //}
            }
            return _position;
        }
    }
}
